*The following is a replication file for the tabels and models found in
*the article. Please contact Kaitlen Cassell (kaitlen.j.cassell@vanderbilt.edu)
*with requests for additional materials related to the study (such as data
*visualization, coding of variables, robustness checks, or appendix
*materials)


use "CoupReplicationFile.dta", clear

*TABLE 1: First stage analysis, individual level determinants of coup justification
reg jc_dummy q1_new  q2 ed tamano quintall exc7_new aoj11_new ///
psar_new  ing4_new b12_new  m1_new  i.wave##i.pais if !inlist(pais, 6, 7, 22, 40, 41), cluster(pais)

outreg2 using Table1, dec(2) label excel alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, ^) replace

***********************************************************************************
*TABLE 2: Mean national coup justification: Two-step national level model 

*Run the two step model
svy: reg jc_dummy q1_new  q2 ed tamano quintall exc7_new aoj11_new ///
psar_new  ing4_new  b12_new m1_new  i.wave##i.pais if !inlist(pais, 6, 7, 22, 40, 41) 

*Step 2: Then you will run the margins command:
 
*note: 1 # symbol
margins wave#pais, saving(intercepts.dta, replace)
 
*open this dataset and confirm that _m1 is wave & _m2 is pais (should be the order in the margins command)
clear all
use "intercepts.dta"

*adjust rename commands accordingly (only need this step if using merge in Step 3)
rename _m1 wave
rename _m2 pais
save "intercepts.dta", replace
 *This gives you a dataset that is the equivalent of the intercepts. 
 
*reopen the original LAPOP dataset
clear all
use "CoupReplicationFile.dta", clear
 
*Step 3: You want to add any level 2 data. If the level2 data is already in your dataset,
*you can collapse and merge that data into the margins dataset:
sort pais wave
collapse gdppcusd gdppcusd_lag gdppcppp_add gdppcppp_add_lag gdppcppp gdppcgrowth gnipcppp gni_ppp militarygdp ///
age_dem inflat_consum inflat_consum_lag inflat_gdpdef mil_dynamic mil_static gdppcppp_add_log gdppcppp_add_lag_log, by(pais wave)
save "lev2.dta", replace
merge 1:1 pais wave using intercepts.dta
 
*Now you have a dataset with all the intercepts and the level2 data. 
 
*Drop missing values for the years/countries in which question was not asked
drop if _merge!=3
drop _merge
 
*Step 4: Now you can run the model the intercepts as outcomes:
 
*TABLE 3 REGRESSION without logged transformation
reg _margin gdppcppp_add  age_dem, vce(hc3)

*TABLE 3 REGRESSION with logged transformation (as presented in the article)
reg _margin gdppcppp_add_log  age_dem, vce(hc3) 

*run with logged LAGGED transformation
reg _margin gdppcppp_add_lag_log  age_dem, vce(hc3) 



***********************************************************************************
*TABLE 3: Bivariate correlations: Determinants of coup justification over time

*Change from first to last survey wave
corr D_jc_dummyp D_exc7_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.03

corr D_jc_dummyp D_aoj11_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.17

corr D_jc_dummyp D_psar_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.03

corr D_jc_dummyp D_ing4_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.41

corr D_jc_dummyp D_b12_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*.08

corr D_jc_dummyp D_m1_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.11
 
corr D_jc_dummyp D_q1_newp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.14

corr D_jc_dummyp D_edp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.02

corr D_jc_dummyp D_tamanop if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*.08

corr D_jc_dummyp D_quintallp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*.05

*Second level vars
corr D_jc_dummyp D_gdppcpppp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.14

corr D_jc_dummyp D_age_demp if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.14


*Change between each survey wave
corr D_jc_dummy_wave D_exc7_new_wave if !inlist(pais,6,7,22,28,29,40,41) 
*-.15

corr D_jc_dummy_wave D_aoj11_new_wave if !inlist(pais,6,7,22,28,29,40,41) 
*-.02

corr D_jc_dummy_wave D_psar_new_wave if !inlist(pais,6,7,22,28,29,40,41)
*-.26

corr D_jc_dummy_wave D_ing4_new_wave if !inlist(pais,6,7,22,28,29,40,41) 
*-.23

corr D_jc_dummy_wave D_b12_new_wave if !inlist(pais,6,7,22,28,29,40,41)
*-.07

corr D_jc_dummy_wave D_m1_new_wave if !inlist(pais,6,7,22,28,29,40,41) 
*-.16
 
corr D_jc_dummy_wave D_q1_new_wave if !inlist(pais,6,7,22,28,29,40,41) 
*.06

corr D_jc_dummy_wave D_ed_wave if !inlist(pais,6,7,22,28,29,40,41) 
*-.02

corr D_jc_dummy_wave D_tamano_wave if !inlist(pais,6,7,22,28,29,40,41) 
*.17

corr D_jc_dummy_wave D_quintall_wave if !inlist(pais,6,7,22,28,29,40,41) 
*.10

*Second level vars
corr D_jc_dummy_wave D_gdppcppp_wave if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*-.37

corr D_jc_dummy_wave D_age_dem_wave if !inlist(pais,6,7,22,28,29,40,41) & year==2012
*.12
*****************************************************************************************
*Figures

*FIGURE 1: High coup justification in Honduras
*Manually enter data into stata based on the agggregate average coup support in each 
*year for Honduras

use "Figure1DataHonduras.dta", clear

label var var1 "Year"
label var var2 "Average Coup Justification"

graph twoway scatter var2 var1, xlab(2004 2006 2008 2010 2012 2014, valuelabel angle(30)) ///
 mlabel(var2) mlabp(12) msymbol(d) mcolor(navy) xtitle("Year") ///
ytitle("Average Coup Justification", size (medsmall)) connect(direct) lwidth(vthin) lcolor(navy) ///
ylabel(30(5)75) caption("AmericasBarometer Fieldwork: 2/9-3/29/2008") title("Honduras")

*saved as "FinalFigure1.tif"
______________________________________________________________________________________________

*FIGURE 2: Low coup justification in Ecuador
*Manually enter data into stata based on the agggregate average coup support in each 
*year for Ecuador

use "Figure2DataEcuador.dta", clear

label var var1 "Year"
label var var2 "Average Coup Justification"

graph twoway scatter var2 var1, xlab(2001 2004 2006 2008 2010 2012 2014, valuelabel angle(30)) ///
xline(2002.5, lpattern(dash) lcolor(red)) mlabel(var2) mlabp(12) msymbol(d) mcolor(navy) xtitle("Year") ///
ytitle("Average Coup Justification", size (med)) connect(direct) lwidth(vthin) lcolor(navy) ///
ylabel(30(5)75) caption("AmericasBarometer Fieldwork: 2/2-3/19/2010") title("Ecuador")

*saved as "FinalFigure2.tif"

______________________________________________________________________________________________

*FIGURE 3: averages of jc10 and jc13 by country 

use "CoupsReplicationFile.dta", clear

lp_comparative jc10rr pais if !inlist(pais, 6, 7, 22, 40, 41), sort(jc10rr) xtitle("High Crime") 

lp_comparative jc13rr pais if !inlist(pais, 6, 7, 22, 40, 41), sort(jc13rr) xtitle("High Corruption")

lp_combine, iscale(.7) gr1(lp_comparative jc10rr pais if !inlist(pais, 6, 7, 22, 40, 41), sort(jc10rr) xtitle("High Crime")) ///
gr2(lp_comparative jc13rr pais if !inlist(pais, 6, 7, 22, 40, 41), sort(jc13rr) xtitle("High Corruption"))

*saved as "FinalFigure3.tif"
______________________________________________________________________________________________


*FIGURE 4: LINE GRAPH OF COUP SUPPORT 
*Decided to go with regular scatter instead of LP line to match figures 1-2
*manually entered data

use "Figure4FullSample.dta", clear

label var var1 "Year"
label var var2 "Average Coup Justification (Full Sample)"

graph twoway scatter var2 var1, xlab(2004 2006 2008 2010 2012 2014, valuelabel angle(30)) ///
 mlabel(var2) mlabp(12) msymbol(d) mcolor(navy) xtitle("Year") ///
ytitle("Average Coup Justification", size (med)) connect(direct) lwidth(vthin) lcolor(navy) ///
ylabel(30(10)70)

*saved as "FinalFigure4.tif"

______________________________________________________________________________________________

use "CoupsReplicationFile.dta", clear

*FIGURE 5: coup justification in each country over time

*this models the interaction of wave and pais to estimate the predicted value (mean) of the dependent variable
*for each country in each wave  
foreach i of numlist 1/5 8/18 20/24 {
  quietly: svy: reg jc_dummy q1_new  q2 ed tamano quintall exc7_new aoj11_new ///
psar_new  ing4_new  b12_new m1_new  i.wave##i.pais if !inlist(pais, 6, 7, 22, 40, 41) & country`i'==1
  margins i.wave##i.pais, noatlegend
  marginsplot, recast(scatter) recastci(rspike) allsimp legend(off) ///
   ciopts(lwidth(thick)) xtitle(" ", size(tiny)) ytitle(" ", size(tiny)) ///
   title("`: variable label country`i''", size(medsmall)) ///   
   ylabel(20(10)70, angle(0) labsize(small)) ///
   plotregion(style(none)) xscale(noextend) yscale(noextend) ///
   name(country_`i', replace)
 }
 *note this doesn't estimate SEs if cluster is specified, so I went with svy instead 

 
 *All countries in one graph (have to manually update the title for each graph to not include pais==)
graph combine country_1 country_2 country_3 country_4 country_5 country_8 ///
country_9 country_10 country_11 country_12 country_13 country_14 country_15 country_16 ///
country_17 country_18  country_20 country_21 country_22  country_23 country_24

______________________________________________________________________________________________
______________________________________________________________________________________________
______________________________________________________________________________________________
